
package edu.moravian.UI;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;

/**
 *
 * @author ronaghan
 * 
 * Find images here http://gratuitousspacebattles.wikia.com/wiki/Special:NewFiles
 */
public class ImageManager
{
    private static final String BASE_PATH = "src/main/resources/images";

    private static final String BACKGROUND_PATH = "background";
    private static final String TOWERS_PATH = "towers";
    private static final String ENEMIES_PATH = "enemies";
    private static final String ORDINANCES_PATH = "ordinances";
    
    private static BufferedImage background;
    private static HashMap<String, BufferedImage> towers;
    private static HashMap<String, BufferedImage> enemies;
    private static HashMap<String, BufferedImage> ordinances;
    
    
    static 
    {
        init();
    }
    
    private static void init()
    {
        towers = new HashMap<String, BufferedImage>();
        enemies = new HashMap<String, BufferedImage>();
        ordinances = new HashMap<String, BufferedImage>();
        
        try
        {
            //load the background file (there should only be one file in the directory 
            File backgroundDir = new File(BASE_PATH, BACKGROUND_PATH);
            for(File file: backgroundDir.listFiles())
            {
                if(isImage(file))
                {
                    background = ImageIO.read(file);
                }
            }
            
            //load all the tower images
            File towerDir = new File(BASE_PATH, TOWERS_PATH);
            for(File file: towerDir.listFiles())
            {
                if(isImage(file))
                {
                    BufferedImage tower = ImageIO.read(file);
                    towers.put(getFileName(file), tower);
                }
            }
            
            //load all the enemies images
            File enemyDir = new File(BASE_PATH, ENEMIES_PATH);
            for(File file: enemyDir.listFiles())
            {
                if(isImage(file))
                {
                    BufferedImage enemy = ImageIO.read(file);
                    enemies.put(getFileName(file), enemy);
                }
            }
            
            
           
            //load all ordinances
            File orderinancePath = new File(BASE_PATH, ORDINANCES_PATH);
            for(File file: orderinancePath.listFiles())
            {
                if(isImage(file))
                {
                    BufferedImage ordinance = ImageIO.read(file);
                    ordinances.put(getFileName(file), ordinance);
                }
            }
            
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
            System.exit(-1);
        }
        
    }
    
    private static boolean isImage(File file)
    {
        if(file.canRead() && file.isFile())
        {
            if(file.getName().endsWith(".png") || file.getName().endsWith(".jpg"))
            {
                return true;
            }
        }
        return false;
    }
    
    public static BufferedImage getBackground()
    {
        return background;
    }
    
    public static BufferedImage getTower(String tower)
    {
        return towers.get(tower);
    }
    
    public static BufferedImage getEnemy(String enemy)
    {
        return enemies.get(enemy);
    }
    
    public static BufferedImage getOrdinance(String orderinace)
    {
        return ordinances.get(orderinace);
    }
    
    private static String getFileName(File file)
    {
        
        String s =  file.getName().substring(0, file.getName().lastIndexOf("."));
                
        return s;
    }
}



